#include "general.h"
#if (SENSOR_TYPE == Sensor_MI5100)
#include "pv_api.h"
#include "sensor_cdsp.h"
#include "sp1k_aaa_api.h"
#define HAL_FRONT_REMOVE_UNCALL_FUNCTION

UINT8 sensorDriverGainClass = 0xff;
UINT8	RawR[256],RawG[256],RawB[256];
UINT8 last_gain;
UINT32 last_shutter;

#define MAX_FRAME_RATE_30_IDX 120	// New limited for OV3630 - Jim@0424
#define MIN_FRAME_RATE_30_IDX 26	// New limited for OV3630 - Jim@0424
#define MAX_FRAME_RATE_15_IDX 120	// New limited for OV3630 - Jim@0424
#define MIN_FRAME_RATE_15_IDX 26	// New limited for OV3630 - Jim@0424
// New AE table for OV3630 turnkey - Jim@0424
code AEstr LVtbl60[121] =
	{
		{	50	,300	,	47	,	0	}	,
		{	51	,300	,	47	,	0	}	,
		{	52	,300	,	47	,	0	}	,
		{	53	,300	,	47	,	0	}	,
		{	54	,300	,	47	,	0	}	,
		{	55	,300	,	47	,	0	}	,
		{	56	,300	,	47	,	0	}	,
		{	57	,300	,	47	,	0	}	,
		{	58	,300	,	47	,	0	}	,
		{	59	,300	,	47	,	0	}	,
		{	60	,300	,	47	,	0	}	,
		{	61	,300	,	47	,	0	}	,
		{	62	,300	,	47	,	0	}	,
		{	63	,300	,	47	,	0	}	,
		{	64	,300	,	47	,	0	}	,
		{	65	,300	,	47	,	0	}	,
		{	66	,300	,	47	,	0	}	,
		{	67	,300	,	47	,	0	}	,
		{	68	,300	,	47	,	0	}	,
		{	69	,300	,	47	,	0	}	,
		{	70	,300	,	47	,	0	}	,
		{	71	,300	,	47	,	0	}	,
		{	72	,300	,	47	,	0	}	,
		{	73	,300	,	47	,	0	}	,
		{	74	,300	,	45	,	0	}	,
		{	75	,300	,	43	,	0	}	,
		{	76	,300	,	40	,	0	}	,
		{	77	,300	,	38	,	0	}	,
		{	78	,300	,	36	,	0	}	,
		{	79	,300	,	34	,	0	}	,
		{	80	,300	,	32	,	0	}	,
		{	81	,300	,	30	,	0	}	,
		{	82	,300	,	29	,	0	}	,
		{	83	,300	,	28	,	0	}	,
		{	84	,300	,	26	,	0	}	,
		{	85	,300	,	25	,	0	}	,
		{	86	,300	,	23	,	0	}	,
		{	87	,300	,	21	,	0	}	,
		{	88	,300	,	19	,	0	}	,
		{	89	,300	,	17	,	0	}	,
		{	90	,300	,	16	,	0	}	,
		{	91	,300 ,	14	,	0	}	,
		{	92	,300 ,	12	,	0	}	,
		{	93	,300 ,	11	,	0	}	,
		{	94	,300 ,	10	,	0	}	,
		{	95	,300 ,	9	,	0	}	,
		{	96	,300 ,	7 	,	0	}	,
		{	97	,400 ,	11	,	0	}	,
		{	98	,400 ,	10	,	0	}	,
		{	99	,400 ,	9 	,	0	}	,
		{	100	,400 ,	8 	,	0	}	,
		{	101	,400 ,	7 	,	0	}	,
		{	102	,400 ,	6 	,	0	}	,
		{	103	,400 ,	5 	,	0	}	,
		{	104	,400 ,	4 	,	0	}	,
		{	105	,600 ,	9 	,	0	}	,
		{	106	,600,	8	,	0	}	,
		{	107	,1200,	21	,	0	}	,
		{	108	,1200,	19	,	0	}	,
		{	109	,1200,	17	,	0	}	,
		{	110	,1200,	15	,	0	}	,
		{	111	,1200 ,	14	,	0	}	,
		{	112	,1200 ,	12	,	0	}	,
		{	113	,1200 ,	10	,	0	}	,
		{	114	,1200 ,	9	,	0	}	,
		{	115	,1200 ,	8 	,	0	}	,
		{	116	,1206 ,	7 	,	0	}  ,
		{	117	,1228 ,	7 	,	0	}  ,
		{	118	,1228 ,	6 	,	0	}  ,
		{	119	,1407 ,	7 	,	0	}  ,
		{	120	,1501 ,	7 	,	0	}  ,
		{	121	,1689 ,	8 	,	0	}  ,
		{	122	,1732 ,	7 	,	0	}  ,
		{	123	,1732 ,	6 	,	0	}  ,
		{	124	,1826 ,	6 	,	0	}  ,
		{	125	,1826 ,	5 	,	0	}  ,
		{	126	,2815 ,	11	,	0	}  ,
		{	127	,2815 ,	10	,	0	}  ,
		{	128	,3071 ,	10	,	0	}  ,
		{	129	,3071 ,	9 	,	0	}  ,
		{	130	,3378 ,	9 	,	0	}  ,
		{	131	,3378 ,	8 	,	0	}   ,
		{	132	,3378 ,	7 	,	0	}   ,
		{	133	,3753 ,	7	,	0	}   ,
		{	134	,4504 ,	9	,	0	}	,
		{	135	,4504 ,	8	,	0	}	,
		{	136	,4826 ,	8	,	0	}	,
		{	137	,5197 ,	8	,	0	}	,
		{	138	,5630 ,	8	,	0	}	,
		{	139	,5630 ,	7	,	0	}	,
		{	140	,6142 ,	7	,	0	}	,
		{	141	,6756 ,	8	,	0	}	,
		{	142	,6142 ,	5	,	0	}	,
		{	143	,7507 ,	7	,	0	}	,
		{	144	,7507 ,	6 	,	0	}	,
		{	145	,7507 ,	5 	,	0	}	,
		{	146	,11261,	10	,	0	}	,
		{	147	,11261,	9 	,	0	}	,
		{	148	,11261,	8 	,	0	}	,
		{	149	,13513,	9 	,	0	}	,
		{	150	,11261,	5 	,	0	}	,
		{	151	,13513,	7 	,	0	}	,
		{	152	,13513,	6 	,	0	}	,
		{	153	,13513,	5 	,	0	}	,
		{	154	,13513,	4 	,	0	}	,
		{	155	,16891,	6 	,	0	}	,
		{	156	,16891,	5 	,	0	}	,
		{	157	,16891,	4 	,	0	}	,
		{	158	,22522,	9 	,	0	}	,
		{	159	,22522,	8 	,	0	}	,
		{	160	,22522,	7 	,	0	}	,
		{	161	,33780,	12 	,	0	}	,
		{	162	,33780,	11 	,	0	}	,
		{	163	,33780,	10 	,	0	}	,
		{	164	,33780,	9 	,	0	}	,
		{	165	,33780,	8 	,	0	}	,
		{	166	,33780,	7	,	0	}	,
		{	167	,33780,	6 	,	0	}	,
		{	168	,33780,	5 	,	0	}	,
		{	169	,33780,	4 	,	0	}	,
		{	170	,33780,	3	,	0	}
	}	;

#define MAX_FRAME_RATE_25_IDX 120				//Jim@0516 Update 50Hz AE Table
#define MIN_FRAME_RATE_25_IDX 28				//Jim@0516 Update 50Hz AE Table
#define MAX_FRAME_RATE_12P5_IDX 120				//Jim@0516 Update 50Hz AE Table
#define MIN_FRAME_RATE_12P5_IDX 28				//Jim@0516 Update 50Hz AE Table

code AEstr LVtbl50[121] =					//Jim@0516 Update 50Hz AE Table
	{
		{	50	,	333	,	47	,	0	}	,
		{	51	,	333	,	47	,	0	}	,
		{	52	,	333	,	47	,	0	}	,
		{	53	,	333	,	47	,	0	}	,
		{	54	,	333	,	47	,	0	}	,
		{	55	,	333	,	47	,	0	}	,
		{	56	,	333	,	47	,	0	}	,
		{	57	,	333	,	47	,	0	}	,
		{	58	,	333	,	47	,	0	}	,
		{	59	,	333	,	47	,	0	}	,
		{	60	,	333	,	47	,	0	}	,
		{	61	,	333	,	47	,	0	}	,
		{	62	,	333	,	47	,	0	}	,
		{	63	,	333	,	47	,	0	}	,
		{	64	,	333	,	47	,	0	}	,
		{	65	,	333	,	47	,	0	}	,
		{	66	,	333	,	47	,	0	}	,
		{	67	,	333	,	47	,	0	}	,
		{	68	,	333	,	47	,	0	}	,
		{	69	,	333	,	47	,	0	}	,
		{	70	,	333	,	47	,	0	}	,
		{	71	,	333	,	47	,	0	}	,
		{	72	,	333	,	47	,	0	}	,
		{	73	,	333	,	47	,	0	}	,
		{	74	,	333	,	47	,	0	}	,
		{	75	,	333	,	47	,	0	}	,
		{	76	,	333	,	44	,	0	}	,
		{	77	,	333	,	42	,	0	}	,
		{	78	,	333	,	39	,	0	}	,
		{	79	,	333	,	37	,	0	}	,
		{	80	,	333	,	35	,	0	}	,
		{	81	,	333	,	33	,	0	}	,
		{	82	,	333	,	31	,	0	}	,
		{	83	,	333	,	30	,	0	}	,
		{	84	,	333	,	28	,	0	}	,
		{	85	,	333	,	27	,	0	}	,
		{	86	,	333	,	25	,	0	}	,
		{	87	,	333	,	24	,	0	}	,
		{	88	,	333	,	22	,	0	}	,
		{	89	,	333	,	20	,	0	}	,
		{	90	,	333	,	18	,	0	}	,
		{	91	,	333 ,	16	,	0	}	,
		{	92	,	333 ,	15	,	0	}	,
		{	93	,	333 ,	13	,	0	}	,
		{	94	,	333 ,	12	,	0	}	,
		{	95	,	333 ,	10	,	0	}	,
		{	96	,	333 ,	9 	,	0	}	,
		{	97	,	333 ,	8 	,	0	}	,
		{	98	,	333 ,	7 	,	0	}	,
		{	99	,	333 ,	6 	,	0	}	,
		{	100	,	333 ,	5 	,	0	}	,
		{	101	,	333 ,	4 	,	0	}	,
		{	102	,	500 ,	9 	,	0	}	,
		{	103	,	500 ,	8 	,	0	}	,
		{	104	,	500 ,	7 	,	0	}	,
		{	105	,	500 ,	6 	,	0	}	,
		{	106	,	1000,	19	,	0	}	,
		{	107	,	1000,	17	,	0	}	,
		{	108	,	1000,	15	,	0	}	,
		{	109	,	1000,	14	,	0	}	,
		{	110	,	1000,	12	,	0	}	,
		{	111	,	1000 ,	11	,	0	}	,
		{	112	,	1023 ,	10	,	0	}  ,
		{	113	,	1055 ,	9 	,	0	}  ,
		{	114	,	1126 ,	9 	,	0	}  ,
		{	115	,	1206 ,	9 	,	0	}  ,
		{	116	,	1206 ,	8 	,	0	}  ,
		{	117	,	1228 ,	7 	,	0	}  ,
		{	118	,	1228 ,	6 	,	0	}  ,
		{	119	,	1407 ,	7 	,	0	}  ,
		{	120	,	1501 ,	7 	,	0	}  ,
		{	121	,	1689 ,	8 	,	0	}  ,
		{	122	,	1732 ,	7 	,	0	}  ,
		{	123	,	1732 ,	6 	,	0	}  ,
		{	124	,	1826 ,	6 	,	0	}  ,
		{	125	,	1826 ,	5 	,	0	}  ,
		{	126	,	2815 ,	11	,	0	}  ,
		{	127	,	2815 ,	10	,	0	}  ,
		{	128	,	3071 ,	10	,	0	}  ,
		{	129	,	3071 ,	9 	,	0	}  ,
		{	130	,	3378 ,	9 	,	0	}  ,
		{	131	,	3378 ,	8 	,	0	}   ,
		{	132	,	3378 ,	7 	,	0	}   ,
		{	133	,	3753 ,	7	,	0	}	,
		{	134	,	4504 ,	9	,	0	}	,
		{	135	,	4504 ,	8	,	0	}	,
		{	136	,	4826 ,	8	,	0	}	,
		{	137	,	5197 ,	8	,	0	}	,
		{	138	,	5630 ,	8	,	0	}	,
		{	139	,	5630 ,	7	,	0	}	,
		{	140	,	6142 ,	7	,	0	}	,
		{	141	,	6756 ,	8	,	0	}	,
		{	142	,	6142 ,	5	,	0	}	,
		{	143	,	7507 ,	7	,	0	}	,
		{	144	,	7507 ,	6 	,	0	}	,
		{	145	,	7507 ,	5 	,	0	}	,
		{	146	,	11261,	10	,	0	}	,
		{	147	,	11261,	9 	,	0	}	,
		{	148	,	11261,	8 	,	0	}	,
		{	149	,	13513,	9 	,	0	}	,
		{	150	,	11261,	5 	,	0	}	,
		{	151	,	13513,	7 	,	0	}	,
		{	152	,	13513,	6 	,	0	}	,
		{	153	,	13513,	5 	,	0	}	,
		{	154	,	13513,	4 	,	0	}	,
		{	155	,	16891,	6 	,	0	}	,
		{	156	,	16891,	5 	,	0	}	,
		{	157	,	16891,	4 	,	0	}	,
		{	158	,	22522,	9 	,	0	}	,
		{	159	,	22522,	8 	,	0	}	,
		{	160	,	22522,	7 	,	0	}	,
		{	161	,	33780,	12 	,	0	}	,
		{	162	,	33780,	11 	,	0	}	,
		{	163	,	33780,	10 	,	0	}	,
		{	164	,	33780,	9 	,	0	}	,
		{	165	,	33780,	8 	,	0	}	,
		{	166	,	33780,	7	,	0	}	,
		{	167	,	33780,	6 	,	0	}	,
		{	168	,	33780,	5 	,	0	}	,
		{	169	,	33780,	4 	,	0	}	,
		{	170	,	33780,	3	,	0	}
	}	;

#define MAX_GAIN_IDX (47)
#define MIN_GAIN_IDX 3

code UINT8 gaintbl[48] = {
	 8,9,10,11,12,  13,14,15,16,17,  18,19,20,21,22,  23,24,25,26,27,  28,29,30,31,32,
	 81,82,83,84,85,  86,87,88,89,90,  91,92,93,94,95,  96,97,98,99,100,  101,102,103
};

#define K_GAIN_Large        45
#define K_GAIN_Middle		30
#define K_GAIN_Small		15

code UINT16 cmt[3][9] = {
	{0x0058,0x03D7,0x0011,0x03DF,0x007A,0x03E7,0x03EB,0x03DA,0x007B},
	{0x0045,0x03EF,0x000C,0x03E8,0x006B,0x03EC,0x03F0,0x03F4,0x005C},
	{0x0029,0x0010,0x0006,0x03F4,0x0058,0x03F4,0x03F8,0x0015,0x0033}};


UINT16 ctc_rgain[4]={321,408,492,0};
UINT16 ctc_bgain[4]={479,409,316,0};

#define MCLKMODE        0 /*mclk mode,'1'from I/O Pad,'0'from internal*/
#define PCLKMODE        1 /*pclk mode,'1'from I/O Pad,'0'from internal*/
#define PREV_MCLKDIV    4 /*MCLK div, master clock = (96MHz / mclkdiv)*/
#define PREV_PCLKDIV    1 /*pixel clock = (master clock) / pclkdiv*/
#define SNAP_MCLKDIV    3 /*MCLK div, master clock = (96MHz / mclkdiv)*/
#define SNAP_PCLKDIV    1 /*pixel clock = (master clock) / pclkdiv*/
#if (PCLKMODE == 1)
#define PREV_PCLKFREQ (UINT32) 24000000
#define SNAP_PCLKFREQ (UINT32) 32000000
#define CLK_RATIO     (UINT8) ((SNAP_PCLKFREQ/1000) * 128 / (PREV_PCLKFREQ/1000))
#else
#define PREV_PCLKFREQ (UINT32) 96000000 / PREV_MCLKDIV / PREV_PCLKDIV
#define SNAP_PCLKFREQ (UINT32) 96000000 / SNAP_MCLKDIV / SNAP_PCLKDIV
#define CLK_RATIO     (UINT8) ((UINT16) PREV_MCLKDIV * PREV_PCLKDIV * 128 / SNAP_MCLKDIV / SNAP_PCLKDIV)
#endif
#define PCLKphase 0  /*0~15*/
#define TGGPIOFUNC 0
#define TGGPIODIR 0x04
#define TGGPIOVAL 0

xdata UINT16 exptime;
extern xdata UINT32 G_SHUTTER;
extern xdata UINT8 G_GAIN;
extern xdata UINT8 PreGain;
extern xdata UINT32 PreShutter;
extern UINT8 HAL_CDSPeofWait( UINT8 Number);
extern void cdspColorMatrixRestore_Intr(void);
extern void cdspColorMatrixRestore(void);

void  HAL_MicronI2C_Write(UINT8 RegAddr, UINT8 RegHData, UINT8 RegLData)
{
	UINT8 reg_addr[1], reg_data[2];
	reg_addr[0] = RegAddr;
 	reg_data[0] = RegHData;
	reg_data[1] = RegLData;
 	HAL_WriteSSC(reg_addr, reg_data, 0x02, 0x00);  //Set Seq SSC Write
}

void  HAL_FrontInit (UINT8 CamMode) USING_0
{
	UINT8 tmp0;
	printf (" Initialize the Front Sensor_MI5100 : Enter !!,CamMode=%d \n",CamMode);

	HAL_SetFrontIOConfig(TGGPIOFUNC, 0x00, TGGPIODIR, TGGPIOVAL);
	HAL_SetFrontCLKSource(MCLKMODE, PCLKMODE);
	HAL_SetFrontCLKSet(PREV_MCLKDIV, PREV_PCLKDIV, 0);
	tmp0 = XBYTE[0x20E4];
	tmp0 &= 0xF0;
	tmp0 |= PCLKphase;
	XBYTE[0x20E4] |= tmp0;
	//XBYTE[0x20E3] |= 0x08;

	HAL_SSCConfig(Sensor_Slave_Address, Sensor_SSC_Freq);
		XBYTE[0x2908] = 0x01;
	XBYTE[0x290c] = 0x01;
	XBYTE[0x2914] = 0x22;/**/

	XBYTE[0x2AB0] = 0x01;		      //Reset Sensor interface
	XBYTE[0x2AB0] = 0x00;

	HAL_SensorRegInit();
}

void  HAL_FrontPreviewSet(void) USING_0
{
	HAL_FrontVdWait(0,1);
	HAL_SetFrontCLKSource(MCLKMODE, PCLKMODE);
	HAL_SetFrontCLKSet(PREV_MCLKDIV, PREV_PCLKDIV, 0);
	HAL_SetFrontImgCrop(Sensor_Preview_HOffset, Sensor_Preview_VOffset, Sensor_Preview_HSize, Sensor_Preview_VSize, 0x00);
	HAL_SetFrontInverse(Prev_Hsync_Inverse,Prev_Vsync_Inverse);
	HAL_SetFrontReshape(Sensor_Preview_Hfall, Sensor_Preview_Hrise, Sensor_Preview_HReshen, Sensor_Preview_Vfall, Sensor_Preview_Vrise, Sensor_Preview_VReshen);
	HAL_SetSensorPreview();
	XBYTE[0x2110]=Sensor_Preview_BayerPattern;
}
void  HAL_FrontSnapSet(UINT16 Hsize,UINT16 Vsize) USING_0
{
	HAL_SetFrontCLKSet(SNAP_MCLKDIV, SNAP_PCLKDIV, 0);
	HAL_SetFrontReshape(Sensor_Snap_Hfall, Sensor_Snap_Hrise, Sensor_Snap_HReshen, Sensor_Snap_Vfall, Sensor_Snap_Vrise, Sensor_Snap_VReshen);
	HAL_SetFrontImgCrop(Sensor_Snap_HOffset, Sensor_Snap_VOffset, Hsize, Vsize, 1);
	HAL_SetFrontInverse(Snap_Hsync_Inverse,Snap_Vsync_Inverse);
	XBYTE[0x2110]=Sensor_Snap_BayerPattern;
	PreGain =0;
	PreShutter = 0;
}
void  HAL_SensorRegInit(void)
{
	HAL_Wait(20);
	HAL_MicronI2C_Write(0x0d,0x00,0x01);
  	HAL_Wait(300);
	HAL_MicronI2C_Write(0x0d,0x00,0x00);

	HAL_MicronI2C_Write(0x1e,0x40,0x06);
	HAL_MicronI2C_Write(0x49,0x00,0x00);
	HAL_MicronI2C_Write(0x03,0x07,0x9B);
	HAL_MicronI2C_Write(0x04,0x0a,0x23);
	HAL_MicronI2C_Write(0x05,0x00,0x00);
	HAL_MicronI2C_Write(0x06,0x00,0x1a);
	HAL_MicronI2C_Write(0x2b,0x00,0x08);
	HAL_MicronI2C_Write(0x2c,0x00,0x08);
	HAL_MicronI2C_Write(0x2d,0x00,0x08);
	HAL_MicronI2C_Write(0x2e,0x00,0x08);
	HAL_MicronI2C_Write(0x30,0x00,0x01);
	HAL_MicronI2C_Write(0x27,0x00,0x0b);
	HAL_MicronI2C_Write(0x35,0x00,0x1b);
	HAL_MicronI2C_Write(0x09,0x02,0x6a);
	HAL_MicronI2C_Write(0x08,0x00,0x00);
	HAL_MicronI2C_Write(0x3e,0x80,0xc7);
	HAL_MicronI2C_Write(0x41,0x00,0x00);
	HAL_MicronI2C_Write(0x42,0x00,0x03);
	HAL_MicronI2C_Write(0x43,0x00,0x03);
	HAL_MicronI2C_Write(0x4f,0x00,0x14);
	HAL_MicronI2C_Write(0x70,0x00,0xac);
	HAL_MicronI2C_Write(0x71,0xa7,0x00);
	HAL_MicronI2C_Write(0x72,0xa7,0x00);
	HAL_MicronI2C_Write(0x73,0x0c,0x00);
	HAL_MicronI2C_Write(0x74,0x06,0x00);
	HAL_MicronI2C_Write(0x75,0x56,0x17);
	HAL_MicronI2C_Write(0x76,0x6b,0x57);
	HAL_MicronI2C_Write(0x77,0x6b,0x57);
	HAL_MicronI2C_Write(0x78,0xa5,0x00);
	HAL_MicronI2C_Write(0x79,0xab,0x00);
	HAL_MicronI2C_Write(0x7a,0xa9,0x04);
	HAL_MicronI2C_Write(0x7b,0xa7,0x00);
	HAL_MicronI2C_Write(0x7c,0xa7,0x00);
	HAL_MicronI2C_Write(0x7d,0xff,0x00);
	HAL_MicronI2C_Write(0x7e,0xa9,0x00);
	HAL_MicronI2C_Write(0x7f,0xa9,0x00);
}

void HAL_SetSensorPreview(void) USING_0
{
	//HAL_MicronI2C_Write(0x03,0x07,0x9b);
	//HAL_MicronI2C_Write(0x1e,0x40,0x06);
	HAL_MicronI2C_Write(0x22,0x00,0x03);
	HAL_MicronI2C_Write(0x23,0x00,0x03);
	//HAL_MicronI2C_Write(0x0b,0x00,0x01);
	//HAL_MicronI2C_Write(0x08,0x00,0x00);
}
/**************************************************************************************
*                                                                                     *
*  Function Name : HAL_FrontGainSet                                                     *
*                                                                                     *
*  Purposes      :                                                                    *
*    To control Sensor gain                                                           *
*                                                                                     *
*  Desprictions  :                                                                    *
*    Adjust gain value for sensor                                                     *
*                                                                                     *
*  Arguments     :                                                                    *
*    value : in, gain value to set                                                    *
*    option : in, 0 -> immediate change value                                         *
*                 1 -> synchronize the value to the next Vsync                        *
*                                                                                     *
*  Returns       :                                                                    *
*    Error message                                                                    *
*                                                                                     *
*  See also      :                                                                    *
*                                                                                     *
***************************************************************************************/
UINT8 HAL_FrontGainSet(
    UINT32 value,
    UINT32  option
)USING_1
{
	UINT32 tmp0;
	UINT8 regaddr[1], regdata[2];

	tmp0 = option & 0x01;
	regaddr[0] = 0x35;
	regdata[0] = 0x00;
	regdata[1] = gaintbl[(UINT8) value];
	HAL_WriteSSC(regaddr, regdata, 0x02, 0);
	last_gain= regdata[1];

	//printf("gain = %bu\n",last_gain);
	return SUCCESS;
}

UINT8 HAL_FrontGainSet_Intr(
    UINT32 value,
    UINT32  option
)USING_1
{
	UINT32 tmp0;
	UINT8 regaddr[1], regdata[2];

	tmp0 = option & 0x01;
	regaddr[0] = 0x35;
	regdata[0] = 0x00;
	regdata[1] = gaintbl[(UINT8) value];
	HAL_WriteSSC_Intr(regaddr, regdata, 0x02, 0);
	last_gain= regdata[1] ;
	//printf("gain = %bu\n",last_gain);
	return SUCCESS;
}

/**************************************************************************************
*                                                                                     *
*  Function Name : HAL_FrontExpTimeSet                                                  *
*                                                                                     *
*  Purposes      :                                                                    *
*    To control exposure time                                                         *
*                                                                                     *
*  Desprictions  :                                                                    *
*    Adjust exposure time value                                                       *
*                                                                                     *
*  Arguments     :                                                                    *
*    value : in, expossure time value to set, unit 0.1ms                              *
*    option : in, 0 -> immediate change value                                         *
*                 1 -> synchronize the value to the next Vsync                        *
*                                                                                     *
*  Returns       :                                                                    *
*    Error message                                                                    *
*                                                                                     *
*  See also      :                                                                    *
*                                                                                     *
***************************************************************************************/
UINT32
HAL_FrontExpTimeSet(
	UINT32 value,
	UINT32 option
) USING_1
{
	UINT16 ltotal, ftotal, espline;
	UINT32 pixclk;
	UINT8  regaddr[2], regdata[2];

	ltotal = Sensor_Preview_LineTotal;	/* Read line total */
	ftotal = Sensor_Preview_FrameTotal; /* Read frame total */
	pixclk = PREV_PCLKFREQ;
	last_shutter=value;

	espline = (pixclk * 10) / (ltotal * value);
	option = option & 0x01;
//printf("espline = %d\n",espline);
	regaddr[0] = 0x09;
	regdata[0] = (espline & 0xff00) >> 8;
	regdata[1] = espline & 0xff;
	HAL_WriteSSC(regaddr, regdata, 0x02, option );
	return SUCCESS;
}

UINT32
HAL_FrontExpTimeSet_Intr(
	UINT32 value,
	UINT32 option
) USING_1
{
	UINT16 ltotal, ftotal, espline;
	UINT32 pixclk;
	UINT8  regaddr[2], regdata[2];

	ltotal = Sensor_Preview_LineTotal;	/* Read line total */
	ftotal = Sensor_Preview_FrameTotal; /* Read frame total */
	pixclk = PREV_PCLKFREQ;
	last_shutter=value;

	espline = (pixclk * 10) / (ltotal * value);
	option = option & 0x01;

	//printf("espline = %d\n",espline);

	regaddr[0] = 0x09;
	regdata[0] = (espline & 0xff00) >> 8;
	regdata[1] = espline & 0xff;
	HAL_WriteSSC_Intr(regaddr, regdata, 0x02, option );
	return SUCCESS;
}

/**************************************************************************************
*                                                                                     *
*  Function Name : HAL_FrontFrameRateSet                                                *
*                                                                                     *
*  Purposes      :                                                                    *
*    To control Frame Rate                                                            *
*                                                                                     *
*  Desprictions  :                                                                    *
*    Adjust Frame rate for sensor                                                     *
*                                                                                     *
*  Arguments     :                                                                    *
*    value : in, frame rate value to set                                              *
*    option : in, 0 -> immediate change value                                         *
*                 1 -> synchronize the value to the next Vsync                        *
*                                                                                     *
*  Returns       :                                                                    *
*    Error message                                                                    *
*                                                                                     *
*  See also      :                                                                    *
*                                                                                     *
***************************************************************************************/

UINT32
HAL_FrontFrameRateSet(
    UINT32 value,
    UINT32 option
)
{
	value = value;
	option = option;
#if 0
	UINT32 ltotal, ftotal;
	UINT32 pixclk, tmp0;

	UINT8 regaddr[4], regdata[4];

	ltotal = Sensor_Preview_LineTotal;	/* Read line total */
	ftotal = Sensor_Preview_FrameTotal; /* Read frame total */

	pixclk = PREV_PCLKFREQ;
	option = option & 0x01;

	tmp0=(pixclk * 10) /(ltotal * value);

	if (tmp0 >= ftotal ) {
		tmp0 =tmp0-ftotal;
	}
	else {
	    tmp0 =0;
	};
	regaddr[0] = 0x46;//0x2D;
	regdata[0] = tmp0;
	regaddr[1] = 0x47;//0x2E;
	regdata[1] = tmp0 >> 8;
	HAL_WriteSSC(regaddr, regdata, 0x02, option | 0x02);
#endif
	return 0;
}

void HAL_ExpTransToSnap(void)
{

	UINT16 ltotal, ftotal,espline;
	UINT32 tmp0,pixclk;
	UINT8  regaddr[7], regdata[7];
	UINT8  s_gain,lastt_gain;

	ltotal = Sensor_Snap_LineTotal;						/* Read line total */
	ftotal = Sensor_Snap_FrameTotal;					/* Read frame total */
	pixclk = SNAP_PCLKFREQ;

	espline = (pixclk * 10) / (ltotal * last_shutter);
	s_gain = last_gain;

	if (s_gain == 32)
	{
		lastt_gain = s_gain /2;
		espline = espline*2;
	}
	else if ((s_gain > 81)&&(s_gain <97))
	{
		lastt_gain = s_gain - 64;
		espline = espline*2;
	}
	else
	{
		lastt_gain=s_gain;
		espline=espline;
	}

	cdspColorMatrixRestore();
	XBYTE[0x21D3] = 0x02;
	XBYTE[0x21D0] = 0x02;
	sensorDriverGainClass = 0;

	if (espline >= ftotal ) {
		tmp0 =espline-ftotal;
		espline = ftotal-1;
	}
	else {
		tmp0 =0;
	};
	regaddr[0] = 0x09;
	regdata[0] = (espline >> 8)&0xff;
	regdata[1] = (espline )&0xff;
	HAL_WriteSSC(regaddr, regdata, 0x02,  0x00);
	regaddr[0] = 0x35;
	regdata[0] = (lastt_gain>> 8)&0xff;
	regdata[1] = (lastt_gain)&0xff;
	HAL_WriteSSC(regaddr, regdata, 0x02,  0x00);
}

UINT8 HAL_SnapRaw8(UINT8 mode, UINT8 opt) USING_0
{
    	//UINT8  regaddr[7], regdata[7];
		//UINT16 i;
	mode = mode;

	if( opt == 1 ) {
		HAL_FrontVdWait(1,1);

		HAL_MicronI2C_Write(0x22,0x00,0x00);
		HAL_MicronI2C_Write(0x23,0x00,0x00);
		#if 0
		HAL_MicronI2C_Write(0x45, 0x00);
		HAL_MicronI2C_Write(0x2d, 0x00);
		HAL_MicronI2C_Write(0x2e, 0x00);
		HAL_MicronI2C_Write(0x10, aaaaa);
		HAL_MicronI2C_Write(0x00, ggggg);
		printf("%bu-----%bu----/n",aaaaa,ggggg);
		#else
		HAL_ExpTransToSnap();
		#endif
	}

	HAL_FrontVdWait(0,1); // 20081024 mantis 32544 wenguo
	HAL_CdspWriteSkip( 6, 1 );
	HAL_FrontVdWait(0,1);
	HAL_FrontVdWait(1,1);
	HAL_FrontInputGate(0x11);
	HAL_CDSPeofWait(1);
	HAL_SetSensorPreview();
	return 0;
}

#ifndef HAL_FRONT_REMOVE_UNCALL_FUNCTION
void HalSensorPrePV(
	void
)
{

	HAL_FrontVdWait(0,1);
	HAL_FrontVdWait(0,1);
	return;
}
#endif

#if 1 /*cs added for gain*/
UINT8 HAL_MaxGainIndexGet()
{
	return(MAX_GAIN_IDX);
}
UINT8 HAL_MinGainIndexGet()
{
	return(MIN_GAIN_IDX);
}

UINT8 HAL_MaxExpIndexGet(
UINT16 frame
)
{
	switch (frame) {
		case SP1K_FRAME_RATE_30:
			return(MAX_FRAME_RATE_30_IDX);
			break;
		case SP1K_FRAME_RATE_15:
			return(MAX_FRAME_RATE_15_IDX);
			break;
		case SP1K_FRAME_RATE_25:
			return(MAX_FRAME_RATE_25_IDX);
			break;
		case SP1K_FRAME_RATE_12P5:
			return(MAX_FRAME_RATE_12P5_IDX);
			break;
		default:
			return FAIL;
	};
}

UINT8 HAL_MinExpIndexGet(
UINT16 frame
)
{
	switch (frame) {
		case SP1K_FRAME_RATE_30:
			return(MIN_FRAME_RATE_30_IDX);
			break;
		case SP1K_FRAME_RATE_15:
			return(MIN_FRAME_RATE_15_IDX);
			break;
		case SP1K_FRAME_RATE_25:
			return(MIN_FRAME_RATE_25_IDX);
			break;
		case SP1K_FRAME_RATE_12P5:
			return(MIN_FRAME_RATE_12P5_IDX);
			break;
		default:
			return FAIL;
	};
}

void HAL_ColorSatReduce_Intr(UINT8 idx)	   // reduce color saturation and edge
{

	//HAL_CdspColorMSet_Intr(0x059,(0x1d3+512),0x013,(0x1d7+512),0x079,(0x1ee+512),(0x1e5+512),(0x1cd+512),0x08b);
	//XBYTE[0x21D3] = 0x06;
	//XBYTE[0x21D0] = 0x08;
#if 0
	if(idx > K_GAIN_Large) {
		if( sensorDriverGainClass != 3 ) {
			HAL_CdspColorMSet_Intr(cmt[2][0],cmt[2][1],cmt[2][2],cmt[2][3],cmt[2][4],cmt[2][5],cmt[2][6],cmt[2][7],cmt[2][8]);
			XBYTE[0x21D3] = 0x08;
			XBYTE[0x21D0] = 0x08;

			sensorDriverGainClass = 3;
		}
	}
	else if (idx > K_GAIN_Middle) {
		if( sensorDriverGainClass != 2 ) {
			HAL_CdspColorMSet_Intr(cmt[1][0],cmt[1][1],cmt[1][2],cmt[1][3],cmt[1][4],cmt[1][5],cmt[1][6],cmt[1][7],cmt[1][8]);
			XBYTE[0x21D3] = 0x06;
			XBYTE[0x21D0] = 0x08;

			sensorDriverGainClass = 2;
		}
	}
	else if (idx > K_GAIN_Small) {
		if( sensorDriverGainClass != 1 ) {
			HAL_CdspColorMSet_Intr(cmt[0][0],cmt[0][1],cmt[0][2],cmt[0][3],cmt[0][4],cmt[0][5],cmt[0][6],cmt[0][7],cmt[0][8]); // for hgnoise
			XBYTE[0x21D3] = 0x04;
			XBYTE[0x21D0] = 0x0A;

			sensorDriverGainClass = 1;
		}
	}
	else {
		if( sensorDriverGainClass != 0 ) {
			cdspColorMatrixRestore_Intr();
			XBYTE[0x21D3] = 0x02;
			XBYTE[0x21D0] = 0x02;

			sensorDriverGainClass = 0;
		}
	};
#endif
}
#endif
#endif
